package org.karn.example.content_provider;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.util.Base64;


public class HtmlClass {

    Context mContext;
    Cursor mCursor;
    String top = "<!DOCTYPE NETSCAPE-Bookmark-file-1>" + "\n" +
    "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">" + "\n" +
    "<TITLE>Bookmarks</TITLE>" + "\n" +
    "<H1>Bookmarks</H1>" + "\n" +
    "<DL><p>" + "\n";

    String bottom = "</DL><p>";
    String title;
    String url;
    String favicon;

    public HtmlClass (Context context, Cursor cursor)
    {
        mContext = context;
        mCursor  = cursor;
    }


    public void createBookmarkHtml()
    {
        String path = Environment.getExternalStorageDirectory().toString();
        path = path + "/myHtml.html";
        String full = null;
        FileOutputStream fout = null;
        try
        {
            fout = new FileOutputStream(path);
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }

        int count = mCursor.getCount();
        int size  = mCursor.getColumnCount();

        try
        {
            fout.write(top.getBytes());
        } catch (IOException e1)
        {
            e1.printStackTrace();
        }
        
        for(int i = 0; i < count; i++)
        {
            full = "<DT><A HREF=";
            String[] colNames = mCursor.getColumnNames();
            mCursor.moveToPosition(i);
            for(int j = 0; j < size; j++)
            {
                int colIndex = mCursor.getColumnIndex(colNames[j]);

                switch(colIndex)
                {
                    case 0:
                        //concatenating URL
                        full = full + "\"" + mCursor.getString(0) + "\"";
                        break;
                    case 1:
                        //concatenating favicon
                        byte[] byteArray = mCursor.getBlob(1);
                        if(byteArray != null)
                        {
                            full = full + " ICON=";
                            String base64string = Base64.encodeToString(byteArray, Base64.NO_WRAP);
                            full = full + "\"" + base64string + "\">";
                        }
                        else
                        {
                            full = full + ">";
                        }
                        break;
                    case 2:
                        full = full + mCursor.getString(2);
                        break;
                }
            }
            full = full + "</A>" + "\n";
            try
            {
                fout.write(full.getBytes());
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        
        try
        {
            fout.write(bottom.getBytes());
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        try
        {
            fout.flush();
            fout.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }

    }

}
